<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>       
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="${_resources}/logo.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">                                            
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />  
<title>样式模板</title>
<%@ include file="/WEB-INF/jsp/common/inc.jsp" %>
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap-table.min.css" />
<script type="text/javascript" src="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet" href="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.css" />

<script type="text/javascript">
var styleTemplate;
var userId ="${userId}";
var userName ="${userName}";
var user_id = "${user_id}";
var menuId = "${menuId}";
//样式文档数据分页展示
function getTableData(editer,source){	
	styleTemplate = $("#styleTemplate").bootstrapTable({
		url : '${_baseUrl}/KtTestStyleDocumentTemplateController/selectStyleTemplate?rand='+Math.random(), 
		pagination: true,
		pageList: [5, 10, 20, 50],
  		queryParams: queryParams,
  		columns: [{
       	 field: 'checked',
       	width: 60,
       	 checkbox:true,
       	 formatter : function(value, row, index) {
       		if(userId=="sysadmin"){
                   	return ;
          	 }else{
          		 if(row.userId !=null){
          			if(!((row.user_name==userName)&&row.userId ==user_id)){
	                		return {
	                            disabled: true
	                        };
	                	}else{
	                		return;
	                	}
          		 }else{
          			 if(!(row.user_name==userName)){
	                		return {
	                            disabled: true
	                        };
	                	}else{
	                		return;
	                	}
          		 }
              }
          }
        }, {
            field: 'editerId',
            title: 'editerId',
            visible:false,
            width: 5
        }, {
             field: 'id',
             title: 'id',
             visible:false,
             width: 5
        }, {
             field: 'defaultName',
             title: 'defaultName',
             visible:false, 
             width: 80
             
         },{
             field: 'styleDocumentName',
             title: '样式文档名称',
             width: 300,
             formatter : function(value, row, index) {
            	var str="";
        		str +="<a onClick=\"documentDownload('"+row.id+"');\" title='"+value+"'  class=\"late_on_temp\">"+value+"</a>"; 
        		if(row.defaultName == 0){
        			str+= "<a>(默认)</a>";
        		}
				return str;
            }
             
         },{
             field: 'ifDefault',
             title: '模板类型',
             width: 200,
             formatter : function(value, row, index) {
             	var str="";
             	if(value==1){
             		str="测试项文档模板";
             	}else if(value==2){
             		str="测试用例设计单模板";
             	}else if(value==3){
             		str="测试用例执行单模板";
             	}else if(value==4){
             		str="问题报告单模板";
             	}
 				return str;
             }
         },{
            field: 'editer',
            title: '上传人员',
            width: 65,
            formatter : function(value, row, index) {
            	var str=value;
            	if(value=='sysadmin'){
            		str="平台初始化";
            	}
				return str;
            }
        },{
            field: 'alertDate',
            title: '上传时间',
            width: 200
        }]
    });
	
	function queryParams(params){
		return {
		    pageSize: params.limit,
		    pageNo: params.pn,
		    sort : params.sort,
		    order : params.order,
		    styleDocumentName:$("#docName").val()
	   }
	}
	
}
//判断登录人员是否是admin或者sysadmin
function getLoginInfo(){
	 var path="${_baseUrl}/documentTemplateController/returnLoginInfo";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,     
			dataType:"text",    
			success: function (data) {   
				
			    if(data=="true"||data==true){
			    	getTableData (true,false);
			    }else{
			    	getTableData (false,true);
			    }
			}   
		}); 
	
}

//判断当前登录时是否是文档编写人
function judgeCurrentUse(name){
	var  userName = "${userName}";
	if(userName==name){
		return true;
	}else{
		return false;
	}
}

$(function(){	
	getLoginInfo();
	getMenuPath();
	
});
//设为默认
function makeSure(){
	var objects =  $("#styleTemplate").bootstrapTable('getSelections');
	var id ="";
	var styleName = "";
	for(var i =0;i<objects.length;i++){
		if(id.length>0){
			layer.msg("请选择一个模板");
			return;
			//id+=","+objects[i].id;
		}else{
			id=objects[i].id;
			//styleName = objects[i].
		}
	}
	if(id.length==0){
		layer.msg("请选择样式模板");
		return;
	}
	var path="${_baseUrl}/KtTestStyleDocumentTemplateController/makeSureStyleTemplate";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"id":id      
			},    
			dataType:"json",    
			success: function (data) {   
				layer.msg(data.message,{shift:5,time:1500});
				refreshTable();			
			}   
		}); 
	
}


//删除样式文档
function deleteFun(){
	var objects =  $("#styleTemplate").bootstrapTable('getSelections');
	var id ="";
	for(var i =0;i<objects.length;i++){
		if(id.length>0){
			id+=","+objects[i].id;
		}else{
			id=objects[i].id;
		}
	}
	if(id.length==0){
		layer.msg("请选择样式模板");
		return;
	}
	var options = "确定删除吗？";
	layer.confirm(options,{
		btn:['确定','取消']},
		function (){
			//layer.load(2);
			 var path="${_baseUrl}/KtTestStyleDocumentTemplateController/delStyleTemplate";
			 $.ajax({    
					type: "POST",    
					async: false,    
					url:path,   
					data:{
						"id":id      
					},    
					dataType:"json",    
					success: function (data) {   
						layer.msg(data.message,{shift:5,time:1500});
						refreshTable();			
					}   
				});
		},
		function(){
			return;
		}
	);
	$(".layui-layer-btn").css("text-align","center");
	$(".layui-layer-btn a").css("width","80px");
	$(".layui-layer-btn a").css("border-radius","4px");
	$(".layui-layer-btn a").css("line-height","24px");
	$(".layui-layer-btn .layui-layer-btn0").css("border-color","#3979FF");
	$(".layui-layer-btn .layui-layer-btn0").css("background-color","#3979FF");
	$(".layui-layer-btn .layui-layer-btn1").css("border-color","#3979FF");
	$(".layui-layer-btn .layui-layer-btn1").css("background-color","#fff");
	$(".layui-layer-btn .layui-layer-btn1").css("color","#3979FF");
}
//下载样式文档
function documentDownload(id){
	window.location.href="${_baseUrl}/KtTestStyleDocumentTemplateController/styleDownload?id="+id+"&rand="+Math.random() ;
}

//设置样式文档为默认

function defaultStyle(id){
	var path="${_baseUrl}/styleTemplateController/styleTemplateDefault";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"id":id      
			},    
			dataType:"text",    
			success: function (data) {   
				layer.msg(data);
				refreshTable();			
			}   
	}); 
	
}
//上传样式文档
function addFun(){
	layerIndex =layer.open({
		  type: 2, 
		  title:'新增样式模板',
		  area: ['700px', '300px'],
		  fixed: false, //不固定
		  maxmin: true,
		  shim: true,
		  content: "${_baseUrl}/KtTestStyleDocumentTemplateController/showAddStyleTemplateDefine?rand="+Math.random() ,
		  success: function(){
			  document.onkeydown = function (e) { // 回车提交表单
				    var theEvent = window.event || e;
				    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
				    if (code == 13) {
				    	 $(this).trigger("click");
							return false;
				    }
				}
		   }
		});
}

//文档输出格式管理页面
function docuemntOutTempalteList(){
	layerIndex =layer.open({
		  type: 2, 
		  title:'文档输出格式模板',
		  area: ['1000px', '400px'],
		  fixed: false, //不固定
		  maxmin: true,
		  shim: true,
		  content: "${_baseUrl}/documentOutTemplateController/documentOutTemplateListPage?rand="+Math.random()
		});
}
function setHeight(){
	$("#popIframe").load(function(){
		var oObj = $(this).contents().find('body'),
			oAdd = $(this).contents().find('#add'),
			oEdit = $(this).contents().find('a'),
			oDia = $(this).contents().find('#popDiv');
		$(this).parent().css({'max-height':'500px','height':oObj.height()+40+'px'});
		oAdd.bind('click',function(){
			$('#popIframe',window.document.parent).parent().css({'max-height':'500px','height':'390px'});
		});

		oEdit.each(function(){
			if($(this).html() == '修改'){
				$(this).click(function(){
					$('#popIframe',window.document.parent).parent().css({'max-height':'500px','height':'390px'});
				})
			}
		})
	})
}

//取消样式文档的默认属性
function unDefaultStyle(id){
	var path="${_baseUrl}/styleTemplateController/cancelDefault";
	 $.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
			"id":id      
		},    
		dataType:"text",    
		success: function (data) {   	  
			layer.msg(data);
			refreshTable();			
		}   
	}); 
	
}
/* 关闭弹出框 */
function closeWin(){
	layer.closeAll();
 	//$("#popDiv").dialog('close');
}
function closeStyleTempDiv(){
	layer.close(layerIndex);
}

function refreshTable(){
	styleTemplate.bootstrapTable('refresh');
}
//回车
$(document).keyup(function(event){
	  if(event.keyCode == 13){
		  selectStyleMessage();
	  }
});

function selectStyleMessage(){
	styleTemplate.bootstrapTable('refresh',{url:'${_baseUrl}/KtTestStyleDocumentTemplateController/searchStyleTemplate'});
}
function modify(){
	var objects =  $("#styleTemplate").bootstrapTable('getSelections');
	var id = "";
	if(objects.length > 1){
		layer.msg("只能修改一个模板",{time:1500});
		return;
	}else{
		for(var i = 0;i < objects.length;i++){
			if(id.length > 0){
				id += ","+objects[i].id;
			}else{
				id = objects[i].id;
			}
		}
		if(id.length == 0){
			layer.msg("请选择样式模板");
			return;
		}
		
		layer.open({
			  type: 2, 
			  area: ['700px', '270px'],
	          title: '修改样式模板',
	          shade: 0.3, //遮罩透明度
			  fixed: false, //不固定
			  maxmin: true,
			  shim: true,
			  content: '${_baseUrl}/KtTestStyleDocumentTemplateController/showModifyTemplate?documentId='+id+"&rand="+Math.random(),
			});
	}
	
}

function getMenuPath(){
	var path = "${_baseUrl}/KmGroupDocumentTemplateController/getMenuPath";
	$.ajax({
		type: "POST",
		async: true,
		url:path,
		data:{
			"menuId": menuId
		},
		dataType:"json",
		success: function (data) {
			if(data.code == "1"){
				$("#menuPath").text("当前位置 ："+data.data);
			}else{
				$("#menuPath").text("当前位置 ：");
			}
		},
		error:function(data){
			layer.msg("系统错误,请联系管理员");
		}
	});
}
</script>
<style type="text/css">
.fixed-table-container{border:0px}
table{border:1px solid #ddd;margin-top: 7px!important;}
.fixed-table-header{height:0px}
</style>
</head>
<body>
<div class="submenu">
	<div  class="func_header">
		<span id="menuPath"></span>
	</div>
    <div class="func_bar">
    	<div class="search_container">
	        <div class="func_search">
	        	<div class="func_search_div"></div>
	            <input type="text" id="docName" name="docName" value="${searchValue}" placeholder="请输入关键词" />
	            <a class="search_btn" onclick="selectStyleMessage();" id="search" href="javascript:;"></a>
	        </div>
	    </div>
        <div class="btn_container">
        	<shiro:hasPermission name="KmStyleDocumentTemplateController:showAddStyleTemplateDefine">
			       <a class="blue_btn" href="javascript:;" onclick="addFun();">新增</a>
			</shiro:hasPermission>
			 <%-- <shiro:hasPermission name="KmStyleDocumentTemplateController:showModifyTemplate">
               <a class="blue_btn" id="modify" onclick="modify();">修改</a>
             </shiro:hasPermission> --%>
             <shiro:hasPermission name="KmStyleDocumentTemplateController:delStyleTemplate">
			       <a class="blue_btn" href="javascript:;" onclick="makeSure();">设为默认模板</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="KmStyleDocumentTemplateController:delStyleTemplate">
			       <a class="blue_btn" href="javascript:;" onclick="deleteFun();">删除</a>
			</shiro:hasPermission>
        </div>
    </div>
    <div style="overflow: auto; width:98%;margin: auto;">
    	<table id="styleTemplate" class="table table-bordered table_inline"></table>
	</div>
</div>
</body>
</html>